package com.example.demo.controller;

import com.example.demo.service.HuaweiIoTService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/api/iot")
public class HuaweiIoTController {

    @Resource
    private HuaweiIoTService iotService;

    // 华为IoTDA回调接口（需在华为云配置）
    @PostMapping("/callback")
    public String handleCallback(@RequestBody Map<String, Object> body) {
        String deviceId = (String) body.get("device_id");
        Map<String, Object> serviceData = (Map<String, Object>) body.get("service_data");
        iotService.processDeviceData(deviceId, serviceData);
        return "success";
    }

    // 主动拉取设备数据接口
    @GetMapping("/fetch/{deviceId}")
    public String fetchData(@PathVariable String deviceId) {
        iotService.fetchAndSaveDeviceData(deviceId);
        return "Data fetched for device: " + deviceId;
    }
}